app.controller("chargeDetailCtrl", function ($scope,$interval, $http, chargeDetailService) {

    //----------分页参数------------
    $scope.pageRowSize = 10;
    $scope.curPage = 1;

    //-----------------------------

    $scope.object = {};
    $scope.formMode = false;
    $scope.errors = {};

    //--------------初始化菜单---------------
    $scope.initMenu = function () {
        showSecondMenu('billingManage');
        selectSecondMenu('chargeDetailQuery');
    }
    $scope.initMenu();

    //---------------错误信息--------------
    $scope.requestError = function () {
        $scope.errorMessage = "unknown error!"
    };

    $scope.init = function () {
        $scope.startDate = getDateFunction(0);  //开始日期
        $scope.endDate = getDateFunction(0);    //结束日期
    };

    //查询账单
    $scope.findChargeDetail = function () {
        if(NullOrUndefined($("#startDate").val())){
            promptMsg("请选择开始日期！");
            return false;
        }
        if(NullOrUndefined($("#endDate").val())){
            promptMsg("请选择结束日期！");
            return false;
        }
        if(NullOrUndefined($("#contractUserSel").val())){
            promptMsg("请选择合同用户！");
            return false;
        }
        //开始日期和结束日期间隔最多3个月
        var flag = check3MonthsFunction($("#startDate").val(),$("#endDate").val());
        if(!flag){
            promptMsg("开始日期和结束日期间隔最多3个月！");
            return false;
        }

        $.fn.jqLoading({ height: 100, width: 240, text: "查询中，请稍等...." });
        chargeDetailService.findChargeDetail({
            action: 'findChargeDetail',
            startDate: $("#startDate").val(),
            endDate: $("#endDate").val(),
            contractUserId: $("#contractUserSel").val()
        }, function (data) {
            $.fn.jqLoading("destroy");
            if (data.status == "SUCCESS") {
                $scope.chargeDetails = data.data.chargeDetails;

                //保存列表的查询条件，导出时使用
                $scope.startDateVal = $("#startDate").val();
                $scope.endDateVal = $("#endDate").val();
                $scope.contractUserIdVal = $("#contractUserSel").val();
                //查数据
                chargeDetailService.findDetails({
                    action: 'findDetails',
                    startDate: $scope.startDateVal,
                    endDate: $scope.endDateVal,
                    contractUserId: $scope.contractUserIdVal
                }, function (data) {
                    if (data.status == "SUCCESS") {
                        $scope.scUpDataEntityVOList = data.data.scUpDataEntityVOList;
                    } else {
                        promptMsg(data.errorReason);
                    }
                }, $scope.requestError());
            } else {
                promptMsg(data.errorReason);
            }
        }, $scope.requestError());
    }

    //选择导出格式
    /*$scope.toChoseFileFormat = function (operate) {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行导出！");
            return false;
        }
        $('#choseFileFormatModal').on('show.bs.modal', centerModals("choseFileFormatModal"));
        $("#choseFileFormatModal").modal({
            backdrop: "static",//点击空白处不关闭对话框
            keyboard:false // 按esc键盘不关闭
        });
    }*/
    //导出Word或者PDF格式文件
    $scope.exportFile = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行导出！");
            return false;
        }
        window.location.href = "/chargedetail/word?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;

        /*if($('input[name="fileFormat"]:checked').val() == "2"){
            window.location.href = "/chargedetail/pdf?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
        }else{
            window.location.href = "/chargedetail/word?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
        }
        //关闭modal
        $scope.closeChoseFileFormatModal();*/
    }
    //关闭选择导出格式窗口
    $scope.closeChoseFileFormatModal = function(){
        //恢复默认选项
        $("input:radio[name='fileFormat'][value='1']").prop('checked','checked');
        $("#choseFileFormatModal").modal("hide");
    };

    //打印
    $scope.printFile = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行打印！");
            return false;
        }
        var preData=document.getElementById("printshow").innerHTML;
        document.getElementById("printdata").innerHTML =document.getElementById("printpart").innerHTML;
        document.getElementById("printhead").innerHTML = document.getElementById("printhead1").innerHTML;
        printTable(35);
        document.getElementById("printshow").innerHTML=preData;
    }

    //点击“明细”，弹出账单明细表窗口
    $scope.toDetailModal = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行明细操作！");
            return false;
        }

        $('#detailModal').on('show.bs.modal', centerModals("detailModal"));
        $("#detailModal").modal({
            backdrop: "static",//点击空白处不关闭对话框
            keyboard:false // 按esc键盘不关闭
        });

    }
    //关闭账单明细表窗口
    $scope.closeDetailModal = function(){
        $("#detailModal").modal("hide");
    };
    //导出明细excel
    $scope.exportDetailExcel = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行明细操作！");
            return false;
        }
        window.location.href = "/chargedetail/excel?startDate=" + $scope.startDateVal + "&endDate=" + $scope.endDateVal + "&contractUserId=" + $scope.contractUserIdVal;
    }
    //打印明细excel
    $scope.printDetailFile = function () {
        if(NullOrUndefined($scope.startDateVal) || NullOrUndefined($scope.endDateVal) || NullOrUndefined($scope.contractUserIdVal)){
            promptMsg("请查询后再进行明细操作！");
            return false;
        }
        var preData=document.getElementById("printshow").innerHTML;
        document.getElementById("printdata").innerHTML =document.getElementById("printdetailpart").innerHTML;
        document.getElementById("printhead").innerHTML = document.getElementById("printhead2").innerHTML;
        printTable(35);
        document.getElementById("printshow").innerHTML=preData;
    }
});